export default class HP extends Sprite {

  constructor(type) {
    super();
    this.bg = null;// 背景
    this.bar = null;// 血量进度条
    this.MIN_VALUE = 0.1;
    this.MAX_VALUE = 100;
    this.value = 100;

    this.init(type);
  }

  init(type) {

    this.width = 180;
    this.height = 21;

    this.bg = new Sprite();
    this.bar = new Sprite();

    const texture1 = Laya.loader.getRes("res/hp_bg.png");
    this.bg.graphics.drawTexture(texture1, 0, 0, 180, 21);
    let texture2 = null;
    if(type === HP.HP_TYPE_ENERGY) {// 悬空能量
      this.bar.graphics.drawTexture(Laya.loader.getRes('res/en_bar.png'), 0, 0, 155, 12);
    } else {// 加速能量
      this.bar.graphics.drawTexture(Laya.loader.getRes('res/hp_bar.png'), 0, 0, 155, 12);
    }

    this.bar.x = 15;
    this.bar.y = 2;

    this.addChild(this.bg);
    this.addChild(this.bar);
  }

  changeValue(value) {

    this.value += value;
    if(this.value < this.MIN_VALUE) {
      this.value = this.MIN_VALUE;
    } else if(this.value > this.MAX_VALUE) {
      this.value = this.MAX_VALUE;
    }

    this.bar.scale(this.value / this.MAX_VALUE, 1);
  }
}

HP.HP_TYPE_ENERGY = "hp_type_energy";// 能量类型
HP.HP_TYPE_SPEED = "hp_type_speed";// 速度类型